package com.reone.jsbridgedemo.client;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.lzyzsd.jsbridge.BridgeHelper;
import com.reone.jsbridgedemo.utils.LogUtils;
import com.reone.talklibrary.TalkApp;

/**
 * Created by wangxingsheng on 2018/7/13.
 */
public class JSBridgeClient extends WebViewClient {

    private BridgeHelper bridgeHelper;

    public JSBridgeClient(BridgeHelper bridgeHelper) {
        this.bridgeHelper = bridgeHelper;
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtils.e("onReceivedError " + error.getDescription().toString() + " code:" + error.getErrorCode());
        } else {
            LogUtils.e("onReceivedError < M");
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        LogUtils.d("onPageStarted url:" + url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        bridgeHelper.onPageFinish();
        LogUtils.d("onPageFinished url:" + url);
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        super.onReceivedHttpError(view, request, errorResponse);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            LogUtils.e("onReceivedHttpError code:" + errorResponse.getStatusCode());
        } else {
            LogUtils.e("onReceivedError < LOLLIPOP");
        }
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        super.onReceivedSslError(view, handler, error);
        LogUtils.e("onReceivedSslError url:" + error.getUrl());
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
        LogUtils.d("onLoadResource url:" + url);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (bridgeHelper.shouldOverrideUrlLoading(url)) {
            return true;
        }
        return super.shouldOverrideUrlLoading(view, url);
    }

    @SuppressLint("NewApi")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        LogUtils.d("shouldOverrideUrlLoading url:" + request.getUrl().toString());
        if (bridgeHelper.shouldOverrideUrlLoading(request.getUrl().toString())) {
            return true;
        }
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
            view.getContext().startActivity(intent);
            return true;
        } catch (Exception e) {
            TalkApp.talk(e.getMessage());
        }
        return super.shouldOverrideUrlLoading(view, request);
    }
}
